<template>
  <div style="padding: 10px">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="手机号">
        <el-input v-model="searchObj.mobile" placeholder="手机号" />
      </el-form-item>

      <el-form-item label="用户类型">
        <el-select v-model="searchObj.userType" placeholder="请选择" clearable>
          <el-option label="投资人" value="1" />
          <el-option label="借款人" value="2" />
        </el-select>
      </el-form-item>

      <el-form-item label="用户状态">
        <el-select v-model="searchObj.status" placeholder="请选择" clearable>
          <el-option label="正常" value="1" />
          <el-option label="锁定" value="0" />
        </el-select>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="fetchData()">
        查询
      </el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <el-table :data="list" border style="width: 100%">
      <el-table-column type="index" label="#" width="30"> </el-table-column>
      <el-table-column prop="userType" label="用户类型" width="90">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.userType === 1">投资人</el-tag>
          <el-tag type="success" v-else-if="scope.row.userType === 2">
            借款人
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="mobile" label="手机号" width="120">
      </el-table-column>
      <el-table-column prop="nickName" label="用户名称" width="120">
      </el-table-column>
      <el-table-column prop="idCard" label="身份证号"> </el-table-column>
      <el-table-column prop="integral" label="用户积分" width="100">
      </el-table-column>
      <el-table-column prop="createTime" label="注册时间" width="100">
      </el-table-column>
      <el-table-column prop="bindStatus" label="绑定状态" width="90">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.bindStatus === 0">未绑定</el-tag>
          <el-tag type="success" v-else-if="scope.row.bindStatus === 1"
            >已绑定</el-tag
          >
          <el-tag type="warning" v-else>绑定失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="用户状态" width="80">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.status === 0">锁定</el-tag>
          <el-tag type="success" v-else>正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            :type="scope.row.status === 0 ? 'success' : 'primary'"
            @click="handleLock(scope.row, scope.row.status === 0 ? 1 : 0)"
          >
            {{ scope.row.status === 0 ? "解锁" : "锁定" }}
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleShowLoginRecords(scope.row)"
            >登录日志</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: center; margin-top: 20px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 3, 4, 5]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog title="登录日志" :visible.sync="dialogTableVisible">
      <el-table :data="loginRecords">
        <el-table-column type="index" width="30"></el-table-column>
        <el-table-column property="ip" label="IP"></el-table-column>
        <el-table-column
          property="createTime"
          label="登录时间"
        ></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>
<script>
import userInfoApi from "@/api/userInfo";
export default {
  data() {
    return {
      list: [],
      pageNum: 1,
      pageSize: 3,
      total: 0,
      loginRecords: [],
      dialogTableVisible: false,
      searchObj: {},
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    //重置按钮事件
    resetData() {
      //清空查询条件
      this.searchObj = {};
      //显示第一页 不带条件查询的分页数据
      this.pageNum = 1;
      this.pageSize = 3;
      this.fetchData();
    },
    handleCurrentChange(num) {
      this.pageNum = num;
      this.fetchData();
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.fetchData();
    },
    handleShowLoginRecords(row) {
      //根据row.id查询用户的登录日志，然后弹出对话框
      userInfoApi.getLoginRecords(row.id).then((r) => {
        this.loginRecords = r.data.items;

        this.dialogTableVisible = true;
      });
    },
    handleLock(row, type) {
      //type: 0代表锁定   1代表解锁
      console.log(row.id, type);
      //提交请求访问后端接口：
      userInfoApi.lock(row.id, type).then((r) => {
        this.$message.success("操作成功");
        this.fetchData();
      });
    },
    fetchData() {
      userInfoApi
        .getUserInfos(this.pageNum, this.pageSize, this.searchObj)
        .then((r) => {
          this.list = r.data.page.records;
          this.total = r.data.page.total;
          console.log(this.list);
        });
    },
  },
};
</script>
